package com.qdlc.p2p.manage.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.ScoreTypeService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.ScoreType;
import com.qdlc.p2p.dal.model.ScoreTypeModel;

/**
 * Created by liaoxiangkai on 2015/12/7.
 */
public class ManageScoreRuleAction extends BaseAction<ScoreTypeModel> implements ModelDriven<ScoreTypeModel> {

    @Resource
    private ScoreTypeService scoreTypeService;

    private Map<String, Object> data;

    /**
     * 积分规则
     *
     * @return
     */
    @Action("/modules/user/score/rule/scoreRule")
    public String scoreRule() {
        return "scoreRule";
    }

    /**
     * 积分规则列表
     *
     * @return
     */
    @Action("/modules/user/score/rule/scoreRuleList")
    public void scoreRuleList() throws IOException {
        data = new HashMap<String, Object>();
        PageDataList<ScoreTypeModel> modelList = scoreTypeService.scoreRuleList(model);
        data.put("total", modelList.getPage().getTotal());
        data.put("rows", modelList.getList());
        printJson(getStringOfJpaObj(data));

    }

    /**
     * 积分规则编辑页面
     *
     * @return
     */
    @Action("/modules/user/score/rule/scoreRuleEditPage")
    public String scoreRuleEditPage() {
        ScoreType scoreType = scoreTypeService.findById(model.getId());
        request.setAttribute("scoreRule", scoreType);
        return "scoreRuleEditPage";
    }

    /**
     * 修改积分规则
     */
    @Action("/modules/user/score/rule/doScoreRuleEdit")
    public void doScoreRuleEdit() throws IOException {
        ScoreType scoreType = scoreTypeService.update(model);
        if (null == scoreType) {
            printResult("更新失败！", false);
        } else {
            printWebSuccess();
        }
    }

    /**
     * 开启本条积分规则
     */
    @Action("/modules/user/score/rule/scoreRuleEnable")
    public void scoreRuleEnable() throws IOException {
        int result = scoreTypeService.toggleStatus(model);
        if (result < 1) {
            printWebResult("开启失败！", false);
        } else {
            printWebSuccess();
        }
    }

    /**
     * 关闭本条积分规则
     */
    @Action("/modules/user/score/rule/scoreRuleDisable")
    public void scoreRuleDisable() throws IOException {
        int result = scoreTypeService.toggleStatus(model);
        if (result < 1) {
            printWebResult("禁用失败！", false);
        } else {
            printWebSuccess();
        }

    }

}
